package com.bwjf.dzfp.action.borer;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.bwjf.dzfp.biz.FinancialBiz;
import com.bwjf.dzfp.common.base.action.BaseAction;
import com.bwjf.dzfp.entity.Financial;
import com.bwjf.dzfp.entity.User;

/**
 * 蛀虫区
 * @author ccy
 *
 */
@Controller
public class BorerAction extends BaseAction{
	
	@Autowired
	private FinancialBiz financialBiz;
	
	/**
	 * 转到蛀虫区
	 * 
	 */
	@RequestMapping("/borer/borer")
	public String toborer(HttpServletRequest request,
			HttpServletResponse response) {
		return "borer/borer";
	}
	/**
	 * 转到蛀虫区详情
	 * 
	 */
	@RequestMapping("/borer/borerDetail")
	public String toborerDetail(HttpServletRequest request,
			HttpServletResponse response) {
		return "borer/borerDetail";
	}
	
	/**
	 * 初始化蛀虫区
	 * 
	 */
	@RequestMapping("/borer/initializationBorer")
	public void initializationBorer(HttpServletRequest request,
			HttpServletResponse response) {
		response.setContentType("application/json;charset=utf-8");
		
		User user = (User)request.getSession().getAttribute("User");
		Financial financial = financialBiz.getLastrecord();
		financial.setRecorder(user.getId());
		financial.setRecorder_name(user.getName());
		JSONObject json = new JSONObject();
		json.put("financial", financial);
		
		getWriter(response).print(json.toJSONString());
	}
	/**
	 * 蛀虫区数据保存
	 * 
	 */
	@RequestMapping("/borer/saveinfo")
	public void saveinfo(HttpServletRequest request,HttpServletResponse response, Financial financial) {
		
		financial.setBalance(financial.getBalance()+financial.getMenoy());
		int i = financialBiz.saveFinancial(financial);
		if(i>0){
			
			getWriter(response).print("success");
		}else{
			getWriter(response).print("系统出错，请联系管理员！");
		}
	}
	/**
	 * 
	 * @param traRecordUuid
	 * @return
	 */
	@RequestMapping("/borer/findBorerDetail")
	public void findBorerDetail(HttpServletRequest request, HttpServletResponse response) {
		try {
		response.setContentType("json/html;charset=UTF-8");
		String endDate = request.getParameter("endDate");
		String startDate = request.getParameter("startDate");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");//小写的mm表示的是分钟  
		SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟  
		
			Date endDate1=sdf.parse(endDate);
			Date startDate1=sdf.parse(startDate);
			endDate = f.format(endDate1);
			startDate = f.format(startDate1);
		Financial financial = new Financial();
		financial.setEndDate(endDate);
		financial.setStartDate(startDate);
		List<Financial> list = financialBiz.getrecord(financial);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("rows", list);
		jsonObject.put("total", list.size());
		getWriter(response).print(jsonObject.toJSONString());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
